package com.herench.bs.handler;

import com.alibaba.fastjson.JSON;
import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicChargeRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

@Component
@Handler(code = "102111" ,name="获取诊间缴费列表")
public class LoadAllClinicChargeListHandler extends AbstractMessageHandler {

    @Autowired
    private IClinicChargeRecordDao clinicChargeRecordDao;

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String userName = requestMessage.getArgsPropertyStringValue("user");
        String idCard = requestMessage.getArgsPropertyStringValue("idCard");
        String visitDate = requestMessage.getArgsPropertyStringValue("visitDate");
        String payState = requestMessage.getArgsPropertyStringValue("payState");

        Date monday = null;
        Date sunday = null;
        Date currentDay = null;
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.hasLength(visitDate)) {
            currentDay = DataFormatUtils.parserDay(visitDate);
            calendar.setTime(currentDay);
        }else {
            currentDay = new Date();
        }
        // 获得当前日期是一个星期的第几天
        int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        calendar.add(Calendar.DAY_OF_YEAR, calendar.getFirstDayOfWeek() - day);
        monday = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR,6);
        sunday = calendar.getTime();

        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        Map<String, Object> res = outpatientClinicAgent.loadRegistRecordsForClinicCharge(channelCode, hospitalCode, DataFormatUtils.getSimpleTimeString(monday), DataFormatUtils.getSimpleTimeString(sunday), idCard, userName);
        Map<String,Object> resData = new HashMap<>();
        System.out.println(JSON.toJSON(res));
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) res.get("dateList");
        List<Map<String, Object>> tempList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            for (Map<String, Object> map : dataList) {
                List<Map<String,Object>> clinicList = (List<Map<String, Object>>) map.get("resList");
                if (clinicList!= null && clinicList.size() > 0){
                    for (Map<String,Object> t: clinicList ){

                        Map<String, Object> charge = new HashMap<>(16);
                        charge.put("patientId",(String) map.get("patientId"));
                        charge.put("clinicChargeStatus","2");
                        charge.put("channelCode",channelCode);
                        charge.put("hospitalCode",hospitalCode);
                        List<Map<String, Object>> resChargeList = new ArrayList<>();
                        resChargeList = clinicChargeRecordDao.find(charge);
                        if (resChargeList.size()>0){
                          String  recordId = (String) resChargeList.get(0).get("recordId") ;
                            String  paySeq = (String) resChargeList.get(0).get("paySeq") ;
//            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
//            String patientId = (String) resPatientId.get("patientId");
                            Map<String, Object> resCharge = new HashMap<>(16);
                            resCharge = outpatientClinicAgent.canceleClinicChargePaymentInsurance(channelCode, hospitalCode, recordId, (String) map.get("patientId"), paySeq);
                        }
                        Map<String,Object> subEl  = outpatientClinicAgent.loadAllClinicOrderList(channelCode,hospitalCode,(String) t.get("recordId"),(String) map.get("patientId"),"",userName,payState,"");
                        List<Map<String,Object>> appNoList = (List<Map<String, Object>>) subEl.get("appNoList");
                        if (appNoList!= null && appNoList.size() > 0){
                            for (Map<String,Object> e: appNoList ){
                                e.put("patientName",t.get("name"));
                                e.put("docName",t.get("docName"));
                                e.put("deptName",t.get("deptName"));
                                e.put("visitNo",t.get("visitNo"));
                                e.put("visitDate",t.get("resDate"));
                                e.put("recordId",t.get("recordId"));
                                tempList.add(e);
                            }
                        }
                    }
                }
            }
        }
        resData.put("dataList", tempList);
        responseMessage.setData(resData);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
